//
//  MineCollectViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/10/24.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "MineCollectViewController.h"
#import "LOGoodsTableViewCell.h"
#import "StoreTableViewCell.h"
#import "LOGoodsListModel.h"

@interface MineCollectViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton *selectBtn;
    NSMutableArray *storeArr;
    UITableView *_tab;
}
@property (weak, nonatomic) IBOutlet UIButton *storeBtn;
@property (weak, nonatomic) IBOutlet UIButton *goodsBtn;
@property (strong, nonatomic) LOGoodsListModel *goodsModel;
@property (strong, nonatomic) UIView *footerView;
@property (assign, nonatomic) NSInteger pageNumber;
@end

@implementation MineCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"收藏";
    _goodsModel = [[LOGoodsListModel alloc] init];
    _pageNumber = 1;
    storeArr = [NSMutableArray array];
    selectBtn = _goodsBtn;
    selectBtn.tag = 100;
    [self getNetData:API(MineColGoods)];
    [self createFooterView];
    [self createTableView];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createTableView
{
    _tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 125, WIDTH, HEIGHT-125) style:(UITableViewStylePlain)];
    _tab.delegate = self;
    _tab.dataSource = self;
    _tab.showsVerticalScrollIndicator = NO;
    [_tab setTableFooterView:_footerView];
    [_tab registerNib:[UINib nibWithNibName:@"StoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"StoreTableViewCell"];
    [_tab registerNib:[UINib nibWithNibName:@"LOGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LOGoodsTableViewCell"];
    [self.view addSubview:_tab];
    
    _tab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNumber = 1;
        if (selectBtn.tag == 101) {
            //店铺
            [storeArr removeAllObjects];
            [self getNetData:API(MineColStore)];
            
        }else{
            //商品
            [_goodsModel.data removeAllObjects];
            [self getNetData:API(MineColGoods)];
        }
    }];
    _tab.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNumber ++;
        if (selectBtn.tag == 101) {
            //店铺
            [self getNetData:API(MineColStore)];
            
        }else{
            //商品
            [self getNetData:API(MineColGoods)];
        }
    }];
}

- (void)getNetData:(NSString *)url
{
    _storeBtn.enabled = NO;
    _goodsBtn.enabled = NO;
    [THWManager PostHttp:url Parameters:@{@"userToken":[Utils getUserToken], @"page":[NSNumber numberWithInteger:_pageNumber], @"pageSize":@"20"} Success:^(id responseObject) {
        _storeBtn.enabled = YES;
        _goodsBtn.enabled = YES;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [self.view removeAnyView];
            if (selectBtn.tag == 101) {
                //店铺
                if ([responseObject[@"data"][@"line"] count] != 0) {
                    NSDictionary *dic = [responseObject objectForKey:@"data"];
                    for (NSDictionary *d in dic[@"line"]) {
                        [storeArr addObject:d];
                    }
                }
                if ([responseObject[@"data"][@"shop"] count] != 0) {
                    NSDictionary *dic = [responseObject objectForKey:@"data"];
                    for (NSDictionary *d in dic[@"shop"]) {
                        [storeArr addObject:d];
                    }
                }
                if ([responseObject[@"data"][@"line"] count] == 0&& [responseObject[@"data"][@"shop"] count] == 0) {
                    [self.view Message:@"无更多数据"];
                }
                
            }else{
                //商品
                NSError *error;
                if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                    if ([responseObject[@"data"][@"goods"] count] != 0) {
                        for (NSDictionary *dic in [responseObject objectForKey:@"data"][@"goods"]) {
                            GoodsListModel *model = [[GoodsListModel alloc] initWithDictionary:dic error:&error];
                            [_goodsModel.data addObject:model];
                        }
                    }else{
                        [self.view Message:@"无更多数据"];
                    }
                }else{
                    [self.view Message:responseObject[@"msg"]];
                }
            }
            
            [_tab reloadData];
            [_tab.mj_header endRefreshing];
            [_tab.mj_footer endRefreshing];
        }
    } andFalse:^(NSError *error) {
        _storeBtn.enabled = YES;
        _goodsBtn.enabled = YES;
        [self.view removeAnyView];
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (selectBtn.tag == 101) {
        //店铺
        return storeArr.count;
    }else{
        //商品
        return _goodsModel.data.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectBtn.tag == 101) {
        //店铺
        StoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellConfigureDic:storeArr[indexPath.row]];
        cell.back = ^{
            NSString *str;
            if (storeArr[indexPath.row][@"lineId"]) {
                str = storeArr[indexPath.row][@"lineId"];
            }else if(storeArr[indexPath.row][@"shopId"]){
                str = storeArr[indexPath.row][@"shopId"];
            }
            NSLog(@"%@",str);
        };
        return cell;
    }else{
        //商品
        LOGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOGoodsTableViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[LOGoodsTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LOGoodsTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellConfigureModel:_goodsModel.data[indexPath.row]];
        return cell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (IBAction)chooseType:(UIButton*)sender {
    _pageNumber = 1;
    [selectBtn setTitleColor:[UIColor colorWithHex:0x999999] forState:UIControlStateNormal];
    selectBtn = sender;
    [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    if (sender.tag == 100) {
        //商品
        [_goodsModel.data removeAllObjects];
        [self getNetData:API(MineColGoods)];
    }else{
        //店铺
        [storeArr removeAllObjects];
        [self getNetData:API(MineColStore)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
